//
// Copyright (C) 2011 - 2013  Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.2 with QuantLib 1.2.1
//#include "stdafx.h"
#include "SobolRsg.h"
using namespace Cephei::QL::Math::Randomnumbers;
#undef HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Math::Randomnumbers::CSobolRsg::CSobolRsg (UInt64 dimensionality, Microsoft::FSharp::Core::FSharpOption<QL::Math::Randomnumbers::SobolRsg::DirectionIntegersEnum>^ directionIntegers) 
{
	_pSpinlock = new boost::detail::spinlock ();
    try
    {
#ifdef HANDLE
        _phSobolRsg = NULL;
#endif
        QuantLib::Size _dimensionality = (QuantLib::Size)ValueHelper::Convert (dimensionality); //d
        QuantLib::SobolRsg::DirectionIntegers _directionIntegers = 
            (Microsoft::FSharp::Core::FSharpOption<QL::Math::Randomnumbers::SobolRsg::DirectionIntegersEnum>::IsSome::get (directionIntegers) ? (QuantLib::SobolRsg::DirectionIntegers)directionIntegers->Value : QuantLib::SobolRsg::DirectionIntegers::Jaeckel); //10
        _ppSobolRsg = new boost::shared_ptr<QuantLib::SobolRsg> (new QuantLib::SobolRsg ( _dimensionality,  _directionIntegers ));
        
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Math::Randomnumbers::CSobolRsg::CSobolRsg (boost::shared_ptr<QuantLib::SobolRsg>& childNative, Object^ owner) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phSobolRsg = NULL;
#endif
	_ppSobolRsg = &childNative;
    
}
Cephei::QL::Math::Randomnumbers::CSobolRsg::CSobolRsg (QuantLib::SobolRsg& childNative, Object^ owner) 
{
#ifdef HANDLE
	_phSobolRsg = NULL;
#endif
	_ppSobolRsg = new boost::shared_ptr<QuantLib::SobolRsg> (&childNative);
    
    _SobolRsgOwner = owner;
    
}

Cephei::QL::Math::Randomnumbers::CSobolRsg::CSobolRsg (CSobolRsg^ copy) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phSobolRsg = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppSobolRsg = new boost::shared_ptr<QuantLib::SobolRsg> (copy->GetShared());
        
    }
}
Cephei::QL::Math::Randomnumbers::CSobolRsg::CSobolRsg (PLATFORM::Type^ t) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phSobolRsg = NULL;
#endif
	if (!t->IsSubclassOf(CSobolRsg::typeid))
		throw REFNEW Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Math::Randomnumbers::CSobolRsg::CSobolRsg (QuantLib::Handle<QuantLib::SobolRsg>& childNative, Object^ owner)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phSobolRsg = &childNative;
	_ppSobolRsg = &static_cast<boost::shared_ptr<QuantLib::SobolRsg>>(childNative.currentLink());
    
    _SobolRsgOwner = owner;
}
Cephei::QL::Math::Randomnumbers::CSobolRsg::CSobolRsg (QuantLib::Handle<QuantLib::SobolRsg> childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phSobolRsg = &childNative;
	_ppSobolRsg = &static_cast<boost::shared_ptr<QuantLib::SobolRsg>>(childNative.currentLink());
    
}
#endif
#ifdef STRUCT
Cephei::QL::Math::Randomnumbers::CSobolRsg::CSobolRsg (QuantLib::SobolRsg childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phSobolRsg = NULL;
#endif
	_ppSobolRsg = new boost::shared_ptr<QuantLib::SobolRsg> (new QuantLib::SobolRsg (childNative));
    
}
#endif

Cephei::QL::Math::Randomnumbers::CSobolRsg::~CSobolRsg ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
        _pSpinlock = NULL;
    }
    if (_ppSobolRsg != NULL)
    {
	    delete _ppSobolRsg;
        _ppSobolRsg = NULL;
    }
}
Cephei::QL::Math::Randomnumbers::CSobolRsg::!CSobolRsg ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
    }
    if (_ppSobolRsg != NULL)
    {
	    delete _ppSobolRsg;
    }
}
QuantLib::SobolRsg& Cephei::QL::Math::Randomnumbers::CSobolRsg::GetReference ()
{
    if (_ppSobolRsg == NULL) throw REFNEW NativeNullException ();
	return **_ppSobolRsg;
}
boost::shared_ptr<QuantLib::SobolRsg>& Cephei::QL::Math::Randomnumbers::CSobolRsg::GetShared ()
{
    if (_ppSobolRsg == NULL) throw REFNEW NativeNullException ();
	return *_ppSobolRsg;
}
QuantLib::SobolRsg* Cephei::QL::Math::Randomnumbers::CSobolRsg::GetPointer ()
{
    if (_ppSobolRsg == NULL) throw REFNEW NativeNullException ();
	return &**_ppSobolRsg;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::SobolRsg>& Cephei::QL::Math::Randomnumbers::CSobolRsg::GetHandle ()
{
	if (_phSobolRsg == NULL)
	{
		_phSobolRsg = new Handle<QuantLib::SobolRsg> (*_ppSobolRsg);
	}
	return *_phSobolRsg;
}
#endif
bool Cephei::QL::Math::Randomnumbers::CSobolRsg::HasNative () 
{
	return (_ppSobolRsg != NULL);
}

UInt64 Cephei::QL::Math::Randomnumbers::CSobolRsg::Dimension::get ()
{
    try
    {
    	QuantLib::Size _rv = (QuantLib::Size)(*_ppSobolRsg)->dimension ( );   
        UInt64 _nrv = _rv; //c1
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Math::Randomnumbers::ISobolRsg^ Cephei::QL::Math::Randomnumbers::CSobolRsg_Factory::Create (UInt64 dimensionality, Microsoft::FSharp::Core::FSharpOption<QL::Math::Randomnumbers::SobolRsg::DirectionIntegersEnum>^ directionIntegers)
{
    return REFNEW CSobolRsg ( dimensionality,  directionIntegers);
}
